package com.ruoyi.app.business.check;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.app.config.util.UserUtil;
import com.ruoyi.app.converter.CheckRecordConverter;
import com.ruoyi.app.domain.check.CheckRecord;
import com.ruoyi.app.domain.company.CompanyCheckResult;
import com.ruoyi.app.domain.user.UserCheckResult;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.service.check.entity.CheckRecordEntity;
import com.ruoyi.service.check.enums.CheckRecordStatusEnum;
import com.ruoyi.service.check.enums.CheckRecordTypeEnum;
import com.ruoyi.service.check.query.CheckRecordQuery;
import com.ruoyi.service.check.service.CheckRecordService;
import com.ruoyi.service.user.entity.UserEntity;
import com.ruoyi.service.user.service.UserService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 作用：审核记录
 * </p>
 *
 * @author Zane
 * @since 2025-10-30 4:55
 */
@Component
public class CheckRecordBusiness {

  @Resource
  private CheckRecordService checkRecordService;
  @Resource
  private UserService userService;

  public CheckRecordEntity addCheckRecord(CompanyCheckResult checkResult) {
    final CheckRecordEntity checkRecordEntity = buildCheckRecord(checkResult.getComId(),
        checkResult.getChekResult(),
        checkResult.getComment(),
        CheckRecordTypeEnum.COMPANY);
    checkRecordService.save(checkRecordEntity);
    return checkRecordEntity;
  }

  public void addCheckRecord(UserCheckResult checkResult) {
    checkRecordService.save(buildCheckRecord(checkResult.getUserId(), checkResult.getChekResult(),
        checkResult.getComment(),
        CheckRecordTypeEnum.USER));
  }

  private CheckRecordEntity buildCheckRecord(Long targetId, Integer checkResult, String checkRemark,
      CheckRecordTypeEnum checkRecordTypeEnum) {
    final CheckRecordStatusEnum checkRecordStatusEnum = CheckRecordStatusEnum.valueOfCode(
        checkResult);
    return new CheckRecordEntity()
        .setTargetId(targetId)
        .setCheckResult(checkRecordStatusEnum.getCode())
        .setCheckType(checkRecordTypeEnum.getCode())
        .setCheckByName(UserUtil.getLoginUser().getName())
        .setCheckTime(LocalDateTime.now())
        .setCheckRemark(checkRemark)
        .setCreateBy(UserUtil.getUserId())
        .setCreateTime(LocalDateTime.now())
        .setUpdateBy(UserUtil.getUserId())
        .setUpdateTime(LocalDateTime.now());
  }

  public CommonPageInfo<CheckRecord> listRecord(CheckRecordQuery query) {
    final CommonPageInfo<CheckRecordEntity> pageInfo = PageUtils.startPage(query);
    pageInfo.initData(checkRecordService.listRecord(query));
    final CommonPageInfo<CheckRecord> result = pageInfo.convert(
        CheckRecordConverter.INSTANCE::toCheckRecord);
    if (CollUtil.isNotEmpty(result.getList())) {
      final List<Long> userIds = result.getList().stream()
          .filter(record -> StringUtils.isBlank(record.getCheckByName()))
          .map(CheckRecord::getCreateBy).collect(Collectors.toList());
      final Map<Long, UserEntity> userMap = userService.userMapByIds(userIds);
      result.getList().forEach(record -> {
        record.setCheckResultName(
            CheckRecordStatusEnum.valueOfCode(record.getCheckResult()).getDesc());
        record.setCheckTypeName(CheckRecordTypeEnum.valueOfCode(record.getCheckType()).getDesc());
        if (StringUtils.isBlank(record.getCheckByName())
            && userMap.containsKey(record.getCreateBy())) {
          //补一下用户姓名
          record.setCheckByName(userMap.get(record.getCreateBy()).getName());
        }
      });
    }
    return result;
  }
}
